﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

namespace ImageUploader.domain
{
    public class LDAPConnection
    {
        private string urlLDAP;
    //    private AuthenticationTypes authTypes;

        public string UserName
        {
            get;
            private set;
        }
        public string Password
        {
            get;
            private set;
        }

        public string Domain
        { get; private set; }

        public LDAPConnection(string urlLDAP,string domain,  string username,string password)//, AuthenticationTypes authTypes)
        {
            this.urlLDAP = urlLDAP;
            this.UserName = username;
            this.Password = password;
            this.Domain = domain;
        }


        

        public DirectoryEntry CreateDirectoryEntryWithDomainName()
        {
            return CreateDirectoryEntryWithDomainName("");
        }
        public DirectoryEntry CreateDirectoryEntryWithoutDomainName()
        {
            return CreateDirectoryEntryWithoutDomainName("");
        }
        public DirectoryEntry CreateDirectoryEntryWithDomainName(string pathArgs)
        {
            return CreateDirectoryEntry(pathArgs, true);
        }
        public DirectoryEntry CreateDirectoryEntryWithoutDomainName(string pathArgs)
        { 
           // return new DirectoryEntry(urlLDAP
            return CreateDirectoryEntry(pathArgs, false);
        }
        private DirectoryEntry CreateDirectoryEntry(string pathArgs,bool withDomainName)
        {

            return new DirectoryEntry(urlLDAP + "/" + (string.IsNullOrEmpty(pathArgs) ? "" : pathArgs +
                (!string.IsNullOrEmpty(pathArgs) && withDomainName ? "," : "")) + (withDomainName ? Domain : ""), UserName, Password);
        }      
    }
}
